package com.doctor.windflower_doctor.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewDebug.IntToString;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.actionBeen.AuthCodeAction;
import com.doctor.windflower_doctor.http.callback.HttpRequestCallBack;
import com.doctor.windflower_doctor.http.parser.impl.JsonParser;
import com.doctor.windflower_doctor.http.request.HttpRequest;
import com.doctor.windflower_doctor.http.request.HttpRequestParams;
import com.doctor.windflower_doctor.http.response.HttpResponseInfo;
import com.doctor.windflower_doctor.util.Constent;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class FindPassWord extends BaseActivity implements Constent {
	private EditText authcode_editText, new_password_editText,
			re_new_password_editText;
	private String phoneNumber;
	private Button finish_Button, re_send_Button;
	private TimeCount count;
	private ImageButton back_imageButton;

	@Override
	protected void getSaveData(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void processLogic() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		finish_Button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (authcode_editText.getText().toString().trim().equals("")) {
					Toast.makeText(FindPassWord.this, "验证码不能为空",
							Toast.LENGTH_SHORT).show();
					return;
				}
				if (new_password_editText.getText().toString().trim()
						.equals("")) {
					Toast.makeText(FindPassWord.this, "请输入新密码",
							Toast.LENGTH_SHORT).show();
					return;
				}
				if (re_new_password_editText.getText().toString().trim()
						.equals("")) {
					Toast.makeText(FindPassWord.this, "请重新输入新密码",
							Toast.LENGTH_SHORT).show();
					return;
				}
				if (!new_password_editText
						.getText()
						.toString()
						.trim()
						.equals(re_new_password_editText.getText().toString()
								.trim())) {
					Toast.makeText(FindPassWord.this, "新密码必须一致",
							Toast.LENGTH_SHORT).show();
				} else {
					changePassword();
				}
			}
		});
		back_imageButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		re_send_Button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				v.setBackgroundResource(R.drawable.wait_button);
				reSendAuthCode();
				count.start();
			}
		});
	}

	private void changePassword() {

		HttpMethod requestMethod = HttpMethod.POST;
		HttpRequestCallBack<AuthCodeAction> requestCallBack = new HttpRequestCallBack<AuthCodeAction>(
				new JsonParser(), AuthCodeAction.class) {
			@Override
			protected void handleResult(
					HttpResponseInfo<AuthCodeAction> responseInfo) {
				if (responseInfo.success.success) {
					Toast.makeText(FindPassWord.this, "修改密码成功！！！",
							Toast.LENGTH_SHORT).show();
					startActivity(new Intent(FindPassWord.this,
							MainActivity.class));
					finish();
				} else {
					Toast.makeText(FindPassWord.this,
							responseInfo.success.message, Toast.LENGTH_SHORT)
							.show();
				}

			}

			@Override
			protected void handleError(HttpException e, String msg) {
				Toast.makeText(FindPassWord.this, msg, Toast.LENGTH_SHORT)
						.show();
			}

			@Override
			public void onStart() {
				super.onStart();

			}

		};
		HttpRequestParams params = new HttpRequestParams(requestMethod, URL
				+ CHANGE_PASSWORD, requestCallBack);
		params.addBodyParameter("appsecret", "123123");
		params.addBodyParameter("telephone", phoneNumber);
		params.addBodyParameter("newpwd", re_new_password_editText.getText()
				.toString().trim());
		params.addBodyParameter("code", authcode_editText.getText().toString()
				.trim());
		HttpRequest.send(params);

	}

	private void reSendAuthCode() {

		HttpMethod requestMethod = HttpMethod.POST;
		HttpRequestCallBack<AuthCodeAction> requestCallBack = new HttpRequestCallBack<AuthCodeAction>(
				new JsonParser(), AuthCodeAction.class) {
			@Override
			protected void handleResult(
					HttpResponseInfo<AuthCodeAction> responseInfo) {
				if (responseInfo.success.success) {
					Toast.makeText(FindPassWord.this, "发送验证码才成功！！",
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(FindPassWord.this,
							responseInfo.success.message, Toast.LENGTH_SHORT)
							.show();
				}

			}

			@Override
			protected void handleError(HttpException e, String msg) {
				Toast.makeText(FindPassWord.this, msg, Toast.LENGTH_SHORT)
						.show();
			}

			@Override
			public void onStart() {
				super.onStart();

			}

		};
		HttpRequestParams params = new HttpRequestParams(requestMethod, URL
				+ AUTHCODE_URL, requestCallBack);
		params.addBodyParameter("telephone", phoneNumber);
		params.addBodyParameter("appsecret", "123123");
		HttpRequest.send(params);

	}

	@Override
	protected void findViews() {
		// TODO Auto-generated method stub
		Intent intent = getIntent();
		phoneNumber = intent.getStringExtra("Phonenumber");
		count = new TimeCount(60000, 1000);
		back_imageButton = (ImageButton) findViewById(R.id.back_imageButton);
		authcode_editText = (EditText) findViewById(R.id.authcode_editText);
		new_password_editText = (EditText) findViewById(R.id.new_password_editText);
		re_new_password_editText = (EditText) findViewById(R.id.re_new_password_editText);
		finish_Button = (Button) findViewById(R.id.finish_Button);
		re_send_Button = (Button) findViewById(R.id.re_send_Button);

	}

	@Override
	public int getResLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.find_password;
	}

	class TimeCount extends CountDownTimer {
		public TimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
		}

		@Override
		public void onFinish() {// 计时完毕时触发
			re_send_Button.setText("重新验证");
			re_send_Button.setClickable(true);
		}

		@Override
		public void onTick(long millisUntilFinished) {// 计时过程显示
			re_send_Button.setClickable(false);
			re_send_Button.setText(millisUntilFinished / 1000 + "");
		}
	}
}
